home *** CD-ROM | disk | FTP | other *** search
/ Megaware 1 / Megaware Volume 1.iso / programg / c-tutor / chap12.txt < prev    next >
Text File  |  1990-07-20  |  22KB  |  472 lines

  1.  
  2.  
  3.  
  4.                                                        Chapter 12
  5.                                            FLYAWAY ADVENTURE GAME
  6.  
  7. PLAYING THE GAME
  8. _________________________________________________________________
  9.  
  10. Prior to studying the source code for this game,  ===============
  11. it would be to your advantage to spend some time    FLYAWAY.EXE
  12. playing the game to get familiar with what the    ===============
  13. game does.  Load the file FLYAWAY.EXE and begin
  14. the adventure through the airport.  The
  15. executable file is precompiled for you so you can begin executing
  16. the program before you have to compile and link the whole thing. 
  17. The entire program is composed of 12 files and will take a little
  18. effort on your part to properly compile and execute it, but that
  19. will come later.
  20.  
  21. If you have played adventure games before, sometimes called
  22. interactive fiction, you should begin trying various commands to
  23. find your way through the airport to your proper plane.  If you
  24. have not played before, a few hints are in order concerning how to
  25. play the game.
  26.  
  27. The object of the game is to get to your proper plane on time so
  28. you can fly away to your vacation.  Of course there a few obstacles
  29. and problems along the way and they will be brought up at the
  30. appropriate time, and it will be up to you to solve the puzzles
  31. associated with each problem.  To add a little excitement, you only
  32. have about twenty-five minutes to get to your plane, with each move
  33. taking a minute, so you must hurry.  Of course, just getting to the
  34. plane on time is not enough, there are a few additional
  35. requirements.  You will find what they are as you progress through
  36. the game.  You will probably be required to restart the game many
  37. times before you arrive at your destination unscathed and on time.
  38.  
  39.  
  40. THE METHOD OF PLAY
  41. _________________________________________________________________
  42.  
  43. The method of play is extremely simple.  You simply wander around
  44. the airport looking for things to do and places to go.  You move
  45. around the airport by giving the system commands to go in a
  46. direction with four choices available, north, south, east, or west. 
  47. You can abbreviate any of these four direction commands to the
  48. first letter only, and you can use either upper or lower case.  The
  49. system may move you to another area of the airport, or it may tell
  50. you that you can't go that way.  Try loading the game now and
  51. typing, the four directions once each to see what happens.  If this
  52. is not clear, enter the word help to get you started.
  53.  
  54. In addition to moving around, you can pick up items or ask for more
  55. information in any of the rooms.  Try telling the system to look
  56. around the room and see what additional information it gives you
  57.  
  58.                                                         Page 12-1
  59.  
  60.                               Chapter 12 - Flyaway Adventure Game
  61.  
  62. for each room, some of the clues for solving the puzzle are given
  63. in the clues issued in response to a look command.  Another
  64. important command is inventory which will give you a list of the
  65. items you possess at any given point in time.  Type the word
  66. inventory at this time to see what items you possess.
  67.  
  68. The remainder of the commands consist of two words, a verb and a
  69. noun.  These can be given in either order, since the system is
  70. smart enough to know the difference, and additional words may be
  71. given following the legal words.  If you give the system a command
  72. that is not in its limited vocabulary, it will tell you it doesn't
  73. understand that word.  Try telling the system to drop an item you
  74. possess, or get an item that is located in the room you are
  75. currently in.
  76.  
  77. Several friends have played this game with no more knowledge than
  78. you have been given.  One solved it in 40 minutes, but most took
  79. about an hour to complete the game.  After you play the game for
  80. awhile, return to the text and we will study the source code for
  81. the game.  The entire source code for the game is on your
  82. distribution disk.  The game was purposely kept small so the code
  83. could be easily grasped by a programming student.  There is no
  84. reason the game could not have been made much larger by the
  85. addition of more rooms, items, and traps.
  86.  
  87.  
  88. THE FIRST CLASS - clock
  89. _________________________________________________________________
  90.  
  91. Examine the file named CLOCK.HPP for the          ===============
  92. definition of the clock class.  This is the          CLOCK.HPP
  93. class for the game clock, and only one instance   ===============
  94. of this class will be used.  It will be used for
  95. the object time_of_day defined in line 25 of
  96. FLYAWAY.CPP.
  97.  
  98. The class is very simple, consisting of only two variables, the
  99. hour and the minute, and four methods.  The first method is the
  100. constructor used to initialize the clock to 8:51 as you can see if
  101. you refer to the implementation of this class.  The next two
  102. methods are used to get the current values of the two variables. 
  103. The final method is much more interesting since it does much more. 
  104. It updates the time of day clock and outputs the user prompt to ask
  105. for the next command.  This may not be the best place to output the
  106. user prompt since this class is devoted to the time of day and
  107. associated operations, but this was chosen as the place to do it
  108. since the time of day is part of the user prompt.  You will notice
  109. that the clock was initialized to 8:51, but the first time output
  110. was 8:52 when you played the game.  In order to simplify the coding
  111. later, when we need to decide if we made it to the plane on time,
  112. the time was incremented at the beginning of each game move.  The
  113. time is therefore the same when the command is entered and when it
  114. is executed, hence the time is incremented prior to even the first
  115. output.
  116.  
  117.                                                         Page 12-2
  118.  
  119.                               Chapter 12 - Flyaway Adventure Game
  120.  
  121.  
  122. The clock class is by far the simplest class in the adventure game
  123. and should be simple for you to understand.  After you are sure you
  124. understand it, we will go on to the next class.
  125.  
  126.  
  127.  
  128. THE SECOND CLASS - items
  129. _________________________________________________________________
  130.  
  131. If you examine the files named ITEMS.HPP and      ===============
  132. ITEMS.CPP, you will find the complete                ITEMS.HPP
  133. definitions of the handling of the items that     ===============
  134. you carried around the airport in the game. 
  135. There were exactly four transportable items that
  136. could be located in each room or carried by yourself, the keys, the
  137. candy, the ticket, and the money.  The keys and the money keep you
  138. from getting through security and the ticket and candy are required
  139. to get you safely on the plane and enroute to your destination.
  140.  
  141. The four items are stored in the class named items in the form of
  142. TRUE or FALSE since that is the only required indication.  A TRUE
  143. means the item is located here, and a FALSE means the item is not
  144. here.  The values of TRUE and FALSE are defined in FLYAWAY.H. 
  145. Finally, there are six methods to operate on these items.
  146.  
  147. The first method is a constructor to set all items to FALSE, and
  148. the next two are used to either get a specific item, or drop one. 
  149. The fourth method is used to tell us if the item is located here
  150. and the last two are used to tell us what items are on hand in this
  151. location.  You will notice that the final two are different because
  152. different text was desired depending on whether you are carrying
  153. the item, or it is located in a room somewhere.
  154.  
  155. The #ifndef in line 5 is required because this header file is
  156. included in many of the other files and if it is included more than
  157. once, there will be a multiple definition, and hence an error.  A
  158. class only needs to be defined once, so after it is defined by one
  159. of the includes, the name ITEMSHPP will be defined and any other
  160. defines will be ignored.  This is necessary because of the separate
  161. compilation capability of C++.  This was described in more detail
  162. near the end of chapter 7.
  163.  
  164. The #ifndef in the class clock was not required but was included
  165. in order to make all class definitions the same.
  166.  
  167. This class is used in line 48 of FLYAWAY.CPP to define an object
  168. for the player named personal_items which stores the list of items
  169. the player is carrying around.  It is also used in the class
  170. location as an embedded or nested object to store the items that
  171. are located in each of the 19 locations in the game.
  172.  
  173.  
  174.  
  175.  
  176.                                                         Page 12-3
  177.  
  178.                               Chapter 12 - Flyaway Adventure Game
  179.  
  180. THE FLIGHT AND GATE CLASS - schedule
  181. _________________________________________________________________
  182.  
  183. Examine the files named SCHEDULE.HPP and         ================
  184. SCHEDULE.CPP for our first example of a rather     SCHEDULE.HPP
  185. large class, the one that does the flight and    ================
  186. gate scheduling.  You will find a large number
  187. of variables in this class, and eight methods to
  188. handle the variables.  Instead of a detailed description of each
  189. variable and method, we will only give a brief overview of the
  190. class.
  191.  
  192. Only one object of this class is declared named flight_info in line
  193. 26 of the program named FLYAWAY.CPP.  The constructor initializes
  194. the flight possibilities, and the method named shuffle_gates()
  195. shuffles all gates around if the player arrives at his correct gate
  196. without reading the monitor in the waiting area.  Once the monitor
  197. in the waiting area is read, the flights_frozen variable is made
  198. TRUE.  Likewise, the players destination is changed every play by
  199. the method named shuffle_flights() until the player reads his
  200. ticket invoking the method named list_actual_destination().
  201.  
  202. This class contains the methods to list the data seen on the
  203. monitor, as well as the data seen when invoking the command look
  204. at one of the gates.  Finally, this class contains the method named
  205. check_flight() which searches through the list of requirements to
  206. see if the player has completed all requirements to successfully
  207. reach the final destination for his vacation.
  208.  
  209. You will notice that several of the location objects were required
  210. to be available within this code and are listed as extern in lines
  211. 9 through 21 of the implementation of the class.  The only other
  212. thing to point out is the rest room requirement prior to boarding
  213. the flight.  Line 23 is where the global variable is defined and
  214. initialized, then in line 74 it is set TRUE if the current location
  215. is the rest room, since this is called once during each player
  216. move.  Finally, the state of this variable is checked in line 237
  217. of this file and the appropriate action taken.  You will note that
  218. the main program is not aware that the rest room variable exists
  219. or that anything happens as a result of this variable.  In addition
  220. to information hiding, we may coin a new term, something like
  221. "Information Ignorance", since the main program did not even need
  222. to be aware that there was a requirement to visit the rest room.
  223.  
  224.  
  225.  
  226. THE MOST USED CLASS - location
  227. _________________________________________________________________
  228.  
  229. The file named LOCATION.HPP is the header file   ================
  230. for the class named location.  It is the class     LOCATION.HPP
  231. that controls all of the moves from location to  ================
  232. location.
  233.  
  234.  
  235.                                                         Page 12-4
  236.  
  237.                               Chapter 12 - Flyaway Adventure Game
  238.  
  239. This class is a bit unusual in that most of the stored data is in
  240. the form of pointers to the various entities.  The first four are
  241. the locations to which we will go if we go in one of the four
  242. directions from the current location, moreover they are pointers
  243. to those four locations.  Next we have pointers to two different
  244. character strings associated with this room.  Finally in line 24
  245. we declare the object named list_of_items which is an object of
  246. class items defined earlier.  Note that this is an embedded class,
  247. a class embedded within the location class.  It is not a parent
  248. class which we are inheriting something from.  In fact we are
  249. instantiating an object of class items for use within the room
  250. since the room is allowed to store any combination of the four
  251. items contained in the class named items.
  252.  
  253. There is no constructor used with this class since we choose to
  254. initialize the locations one by one.  Note that a constructor could
  255. have been used even though we would have to refer to some items
  256. prior to their definition because it is permissible to refer to a
  257. pointer to an object before it is declared.  The method named
  258. init() has 6 variable parameters, all of which are pointers,
  259. associated with it which it uses to initialize the first six
  260. variables of this object.  The last variable, an object of class
  261. items, is initialized through use of the constructor associated
  262. with its class.  Referring to lines 81 through 212 of the main
  263. program FLYAWAY.CPP, you will find all of the initialization code
  264. for the 19 objects of class location.  If you drew a map when you
  265. played the game, you will see the interconnections between the
  266. various locations embedded in the initialization statements. 
  267. Notice there is no way to get back to the car from the passenger
  268. drop off area, because presumably the car leaves when you get out
  269. of it.
  270.  
  271. The next method, named move(), returns a pointer to the new
  272. location if a move was legal, otherwise it returns a NULL value. 
  273. The observant student will also notice that there are special cases
  274. involved with getting out of the snack bar and getting through
  275. security.  These are located here because they are part of the move
  276. logic.  If you played the game to the complete conclusion, you
  277. surely had trouble with at least one of these situations.
  278.  
  279. The rest of the methods in this class should be self explanatory
  280. and will not be discussed any further.
  281.  
  282.  
  283. THE LOCATION MESSAGES
  284. _________________________________________________________________
  285.  
  286. Examine the file named MESSAGE.H for a complete   ===============
  287. listing of the messages output to the monitor        MESSAGE.H
  288. when each location was entered.  You will also    ===============
  289. find the text for each of the messages output in
  290. response to a look command in this file.  These
  291. were put into a separate file only for the purpose of reducing the
  292. size of the main file.  It does not reduce the compile time since
  293.  
  294.                                                         Page 12-5
  295.  
  296.                               Chapter 12 - Flyaway Adventure Game
  297.  
  298. these messages are not separately compiled.  They are included into
  299. the file and compiled each time the main file FLYAWAY.CPP is
  300. compiled.  You will note that a few of the messages have no text
  301. at all, only the empty quote marks, but are included in order to
  302. have something for the initialization code to work with.
  303.  
  304.  
  305. THE PROTOTYPES FOR THE STANDARD C CODE
  306. _________________________________________________________________
  307.  
  308. The file named FLYAWAY.H contains the             ===============
  309. definitions for TRUE and FALSE as well as the        FLYAWAY.H
  310. enumerated type defining the legal dictionary of  ===============
  311. words for use in playing the game.  The list was
  312. started at a value of 1 so the value of zero can
  313. be used to indicate that the word in question was not in the
  314. library and hence not a legal word for use with the game.
  315.  
  316. Finally, this file contains the prototypes for all of the functions
  317. that are used in a normal C fashion rather than being parts of an
  318. object.  As much of this problem as was felt practical was broken
  319. up into objects and the remainder was programmed using standard C
  320. functions.  You will find that as you develop object oriented
  321. techniques, you will not program all of any project with objects
  322. but will finally relegate part of it to your old favorite methods.
  323.  
  324. This file, like all other header files, is protected from multiple
  325. inclusion by the #ifndef construct discussed earlier.
  326.  
  327.  
  328. INPUT COMMAND PARSING
  329. _________________________________________________________________
  330.  
  331. The input command parsing routines were not       ===============
  332. defined as objects but were written as standard     COMMAND.CPP
  333. C++ code to illustrate that all of a program      ===============
  334. need not be handled as objects.  These routines
  335. were written in a separate file to indicate that
  336. it is legal and possible to do so in a program that is primarily
  337. an object oriented program.
  338.  
  339. The code is straightforward and simple to understand if you study
  340. it, so only a few comments will be made about this file.  The
  341. function get_command() reads two words from the keyboard by calling
  342. the function read_a_line() and returns the words if they are a
  343. valid verb and noun, otherwise it returns zeros for the two words.
  344.  
  345. There are four functions at the end of this file that are used to
  346. determine if a word is a verb, a noun, a direction, or an
  347. operation.  These are called upon from various places within the
  348. program.  They should be easy for you to understand.
  349.  
  350.  
  351.  
  352.  
  353.                                                         Page 12-6
  354.  
  355.                               Chapter 12 - Flyaway Adventure Game
  356.  
  357. THE MAIN PROGRAM
  358. _________________________________________________________________
  359.  
  360. We finally reach the main program, the one that   ===============
  361. actually does the top level control.  Examine       FLYAWAY.CPP
  362. the program named FLYAWAY.CPP and we will look    ===============
  363. at some of its interesting characteristics.
  364.  
  365. Beginning with the main() entry point itself, we see that following
  366. a call to initialize() and the output of a few lines of text, we
  367. are into a single do while loop which terminates when the player
  368. enters the word quit or when the verb quit comes up some other way. 
  369. There are other ways to get the quit because it is generated
  370. internally in some cases such as at end of game.
  371.  
  372. The loop itself consists of 6 function or method calls.  The first
  373. method is called to update the time of day clock and output the
  374. players prompt in line 66.  Next we call the function get_command()
  375. to get the players input command.  We perform the required action
  376. in line 68 by calling perform_action() which we will describe in
  377. a few paragraphs.  Finally, we send three messages to the object
  378. named flight_info to shuffle the flights and gates and to check if
  379. the player has reached one of the gates.  Remember that within each
  380. of the methods we perform checks to see if we need to do the thing
  381. requested in the message and either perform the action or simply
  382. return to the caller or message sender.
  383.  
  384.  
  385.  
  386. THE WORKING FUNCTION
  387. _________________________________________________________________
  388.  
  389. The only function we have not mentioned yet is the one that does
  390. all of the work, the function named perform_action() which begins
  391. in line 226.  This function simply looks at the verb and noun, if
  392. there is one, and causes the correct action to be performed. 
  393. Because of the way we packaged all of the other routines, this
  394. function is a snap to implement and to study.  If you go through
  395. each of the else clauses in this function, you will have no trouble
  396. understanding what action is taken for each of the input commands. 
  397. You will notice that many of the actions have conditional clauses
  398. before the action is taken.  For example, it is illegal to buy
  399. candy unless the player has money, the location has candy, and the
  400. location must be the snack_bar according to the rules of the game.
  401.  
  402. Finally, at the end of this function in line 338, we have the
  403. default case if nothing else was attempted.  It is assumed that
  404. there was something funny requested such as a request to get a
  405. monitor.  Both of these are legal words but they make no sense
  406. together.
  407.  
  408.  
  409.  
  410.  
  411.  
  412.                                                         Page 12-7
  413.  
  414.                               Chapter 12 - Flyaway Adventure Game
  415.  
  416. FINAL COMMENTS ON FLYAWAY
  417. _________________________________________________________________
  418.  
  419. Now that you have played the game for awhile and studied the game
  420. in detail, you should have an appreciation for how this game can
  421. be written.  Of course, it could be written in any of several
  422. thousand different ways of packaging and definition.  This has been
  423. only one of the ways.
  424.  
  425. Because the student may be left with the sinking feeling that this
  426. method simply fell out of the sky or was arrived at in some other
  427. esoteric way, it would only be fair to point out that several
  428. earlier attempts at outlining this project were attempted and
  429. rejected prior to this arrangement.  Object oriented programming
  430. requires the same forethought as non-object oriented programming,
  431. but the object oriented compiler will help you in the coding and
  432. debugging phase since the compiler will find and flag many of the
  433. oversight errors we are so good at introducing into our code.  It
  434. was observed during the coding and debugging phase of this project
  435. that in nearly every case, when the program finally got through the
  436. compiler, the program would actually run without bombing out the
  437. system.  This is not always the case using any standard procedural
  438. programming language.
  439.  
  440.  
  441.  
  442.  
  443.  
  444.  
  445.  
  446.  
  447.  
  448.  
  449.  
  450.  
  451.  
  452.  
  453.  
  454.  
  455.  
  456.  
  457.  
  458.  
  459.  
  460.  
  461.  
  462.  
  463.  
  464.  
  465.  
  466.  
  467.  
  468.  
  469.  
  470.  
  471.                                                         Page 12-8
  472.